#include "UVisDataSubscriber.h"

void UVisDataSubscriberComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
}

void UVisDataSubscriberComponent::BeginPlay()
{
	Super::BeginPlay();
}

UFloatDataSubscriber* UVisDataSubscriber::GetFloatSubscriberByIndex(int index)
{
	if (FloatSubscribers_ == nullptr)
		FloatSubscribers_ = MakeUnique<FDataSubscriberContainer<UFloatDataSubscriber>>();
	return FloatSubscribers_->GetSubscriberByIndex(index);
}

UIntDataSubscriber* UVisDataSubscriber::GetIntSubscriberByIndex(int index)
{
	if (IntSubscribers_ == nullptr)
		IntSubscribers_ = MakeUnique<FDataSubscriberContainer<UIntDataSubscriber>>();
	return IntSubscribers_->GetSubscriberByIndex(index);
}

UBoolDataSubscriber* UVisDataSubscriber::GetBoolSubscriberByIndex(int index)
{
	if (BoolSubscribers_ == nullptr)
		BoolSubscribers_ = MakeUnique<FDataSubscriberContainer<UBoolDataSubscriber>>();
	return BoolSubscribers_->GetSubscriberByIndex(index);
}

UStringDataSubscriber* UVisDataSubscriber::GetStringSubscriberByIndex(int index)
{
	if (StringSubscribers_ == nullptr)
		StringSubscribers_ = MakeUnique<FDataSubscriberContainer<UStringDataSubscriber>>();
	return StringSubscribers_->GetSubscriberByIndex(index);
}

UFloatDataSubscriber* UVisDataSubscriberComponent::GetFloatSubscriberByIndex(int index)
{
	return DataSubscriber_.GetFloatSubscriberByIndex(index);
}
UIntDataSubscriber* UVisDataSubscriberComponent::GetIntSubscriberByIndex(int index)
{
	return DataSubscriber_.GetIntSubscriberByIndex(index);
}
UBoolDataSubscriber* UVisDataSubscriberComponent::GetBoolSubscriberByIndex(int index)
{
	return DataSubscriber_.GetBoolSubscriberByIndex(index);
}
UStringDataSubscriber* UVisDataSubscriberComponent::GetStringSubscriberByIndex(int index)
{
	return DataSubscriber_.GetStringSubscriberByIndex(index);
}

template<std::derived_from<UObject> SubscriberT>
inline SubscriberT* FDataSubscriberContainer<SubscriberT>::GetSubscriberByIndex(int index)
{
	if (index < 0)
		return nullptr;

	if (Subscribers_.Num() > index)
	{
		// get:
		return Subscribers_[index];
	}
	else
	{
		// extend array and create new subscriber:
		Subscribers_.SetNum(index + 1);
		Subscribers_[index] = NewObject<SubscriberT>();
		return Subscribers_[index];
	}
}
